﻿/*----------------------------------------------------------------
 * 版权所有 (c) 2025 HaiTangYunchi  保留所有权利
 * CLR版本：4.0.30319.42000
 * 公司名称：
 * 命名空间：SalaryCalculator.Repositories
 * 唯一标识：389c7f31-c031-4f29-a68e-108846a4e97f
 * 文件名：EmployeeRepository
 * 
 * 创建者：海棠云螭
 * 电子邮箱：haitangyunchi@126.com
 * 创建时间：2025/5/10 20:18:51
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite;

namespace SalaryCalculator
{
    /// <summary>
    /// 员工数据访问类
    /// </summary>
    public class EmployeeRepository
    {
        /// <summary>
        /// 获取所有员工
        /// </summary>
        public List<Employee> GetAllEmployees(bool includeInactive = false)
        {
            var employees = new List<Employee>();

            using (var connection = DatabaseHelper.GetConnection())
            {
                connection.Open();

                string query = "SELECT * FROM Employees";
                if (!includeInactive)
                    query += " WHERE IsActive = 1";
                query += " ORDER BY ID"; // 原来是按照名字排序，ORDER BY Name ，现在修改为ID排序

                using (var command = new SQLiteCommand(query, connection))
                {
                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            employees.Add(new Employee
                            {
                                Id = Convert.ToInt32(reader["Id"]),
                                Name = reader["Name"].ToString(),
                                HourlyRate = Convert.ToDecimal(reader["HourlyRate"]),
                                IsActive = Convert.ToInt32(reader["IsActive"]) == 1
                            });
                        }
                    }
                }
            }

            return employees;
        }

        /// <summary>
        /// 添加新员工
        /// </summary>
        public int AddEmployee(Employee employee)
        {
            using (var connection = DatabaseHelper.GetConnection())
            {
                connection.Open();

                string query = @"
                INSERT INTO Employees (Name, HourlyRate, IsActive)
                VALUES (@Name, @HourlyRate, @IsActive);
                SELECT last_insert_rowid();";

                using (var command = new SQLiteCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@Name", employee.Name);
                    command.Parameters.AddWithValue("@HourlyRate", employee.HourlyRate);
                    command.Parameters.AddWithValue("@IsActive", employee.IsActive ? 1 : 0);

                    return Convert.ToInt32(command.ExecuteScalar());
                }
            }
        }

        /// <summary>
        /// 更新员工信息
        /// </summary>
        public void UpdateEmployee(Employee employee)
        {
            using (var connection = DatabaseHelper.GetConnection())
            {
                connection.Open();

                string query = @"
                UPDATE Employees 
                SET Name = @Name, HourlyRate = @HourlyRate, IsActive = @IsActive
                WHERE Id = @Id";

                using (var command = new SQLiteCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@Id", employee.Id);
                    command.Parameters.AddWithValue("@Name", employee.Name);
                    command.Parameters.AddWithValue("@HourlyRate", employee.HourlyRate);
                    command.Parameters.AddWithValue("@IsActive", employee.IsActive ? 1 : 0);

                    command.ExecuteNonQuery();
                }
            }
        }
    }
}